import { mapView } from "@/api/map";
import Point from "@arcgis/core/geometry/Point";
import Graphic from "@arcgis/core/Graphic.js";
import { disMeasurement, areaMeasurement } from "./measure";
import { directions, removeRouteAnalyse } from "./route";
let mapPoint: Point;
let clickHandler: IHandle;
export const addPoint = (): void => {
  clickHandler = mapView.on("click", (e) => {
    mapView.graphics.removeAll();
    mapPoint = e.mapPoint;
    //设置点样式
    const pointSymbol: Object = {
      type: "simple-marker",
      color: [255, 0, 0],
      size: 15,
      outline: {
        color: [255, 255, 255],
        width: 3,
      },
    };
    //创建点
    const point = new Point({
      x: mapPoint.x,
      y: mapPoint.y,
      spatialReference: mapView.spatialReference,
    });
    //添加
    mapView.graphics.add(
      new Graphic({
        geometry: point,
        symbol: pointSymbol,
      })
    );
  });
};
export const clearPoint = (): void => {
  mapView.graphics.removeAll();
  if(clickHandler){
    clickHandler.remove();
  }
  //清除控件的要素
  if (disMeasurement) {
    mapView.ui.remove(disMeasurement);
    disMeasurement.clear();
  }
  //清除控件的要素
  if (areaMeasurement) {
    mapView.ui.remove(areaMeasurement);
    areaMeasurement.clear();
  }
  if (directions) {
    directions.viewModel.reset();
    removeRouteAnalyse();
  }
  mapView.goTo({
    zoom: 9,
    center: [114.52, 38.05],
  });
};
export { mapPoint };
